package com.kzw.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.Clazz;
import com.kzw.entity.Student;
import com.kzw.service.ClazzService;
import com.kzw.service.StudentService;

import my.mvc.RequestContext;
import my.util.ServletUtils;
import my.vo.Page;

/**
 * 一般情况：一个请求对应一个servlet 希望：在一个servlet中处理多个请求
 */
public class StudentAction {

	private StudentService studentService = new StudentService();
	private ClazzService clazzService = new ClazzService();

	/**
	 * 学生列表
	 */
	public void list(RequestContext rc) throws ServletException, IOException {

		String name = rc.param("name");
		System.out.println(name);

		// 分页查找
		int pageNo = rc.param("pageNo", 1);
		Page<Student> page = new Page<>(10);
		page.setPageNo(pageNo);

		studentService.findPage(page);
		rc.reqAttr("page", page);
		rc.forward("/pages/student_list.jsp");
	}

	/**
	 * 增加学生
	 */
	public void save(RequestContext rc) throws ServletException, IOException {
		// 普通字段
		Student student = rc.form(Student.class);

		if (student.getId() == 0) {
			// 增加
			studentService.save(student);
		} else {
			// 修改
			studentService.update(student);
		}

		// 重定向到一个查询请求
		rc.redirect(rc.contextPath() + "/action/student/list");
	}

	/**
	 * 删除学生
	 */
	public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String id = req.getParameter("id");
		if (id != null && !id.equals("")) {
			studentService.remove(Long.parseLong(id));
		}

		// 重定向到一个查询请求
		resp.sendRedirect(req.getContextPath() + "/mvc/student");
	}

	/**
	 * 增加学生之前，用于显示班级下拉列表
	 */
	public void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		List<Clazz> clazzList = clazzService.findAll();
		req.setAttribute("clazzList", clazzList);
		req.getRequestDispatcher("/pages/student_form.jsp").forward(req, resp);
	}

	/**
	 * 编辑学生之前，用于回显
	 */
	public void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String id = req.getParameter("id");
		if (id != null && !id.equals("")) {
			Student student = studentService.findById(Long.parseLong(id));
			req.setAttribute("student", student);

			// 所有的班级
			List<Clazz> clazzList = clazzService.findAll();
			req.setAttribute("clazzList", clazzList);

			req.getRequestDispatcher("/pages/student_form.jsp").forward(req, resp);
		}
	}

	/**
	 * 下载图片
	 */
	public void downImg(RequestContext rc) throws ServletException, IOException {
		long id = rc.param("id", 0L);
		Student student = studentService.findById(id);
		if (student != null) {
			String picPath = student.getPicPath();
			if (StringUtils.isNotBlank(picPath)) {
				// 进行文件下载
				String upload = rc.context().getRealPath("/upload");
				InputStream is = new FileInputStream(new File(upload, picPath));

				HttpServletResponse response = rc.response();
				OutputStream os = response.getOutputStream();
				ServletUtils.setFileDownloadHeader(response, picPath);

				// 进行流的读写，如果文件比较大时
				int len;
				byte[] b = new byte[10240];
				while ((len = is.read(b)) > 0) {
					os.write(b, 0, len);
				}
				is.close();
				os.close();
			}
		}
	}

}
